Put your build triggers into source control with the Cloud Build API

Niel de Wet
2 min readJan 3, 2019

Google Cloud Build triggers are simple enough to configure through the cloud console (and optionally with a cloudbuild.yaml file), but things can quickly get out of hand when you start managing many different build triggers, or worse, many similar triggers. It’s at this point that you wish you had your triggers in code and checked into source control.

In this post I will show how to use the Cloud Build REST API to manage build triggers using their JSON representation.

Scripts

The commands for interacting with the API using cURL are simple enough, but for convenience I have a few scripts. These depend on the following command line utilities: gcloud, bash, jq

List build triggers

List all the build triggers in the project in JSON format.

Parameters: None

Usage:
./list_build_triggers.sh

Create build trigger

Parameters:
*
A JSON file containing a BuildTrigger object

Usage:
./create_build_trigger.sh <build-trigger.json>

Note: After creating a new build trigger, be sure to update the trigger’s JSON file with the ID of the created trigger.

Get build trigger

Get the BuildTrigger JSON definition of a build trigger by ID.

Parameters:
* The UUID of the build trigger

Usage:
./get_build_trigger.sh <existing-build-trigger-ID>

Update build trigger

Parameters:
* A JSON file containing a BuildTrigger object, including the ID of the build trigger to update

Usage:
./update_build_trigger.sh <build-trigger.json>

Delete build trigger

Parameters:
* The UUID of the build trigger to delete

Usage:
./delete_build_trigger.sh <existing-build-trigger-ID>

Example of trigger JSON

The full API resource definition of a BuildTrigger can be found in the documentation.

{
"id": "967e92df-fb2b-49e0-8e0e-b95d5dbeca2a",
"createTime": "2018-12-21T12:34:53.396042093Z",
"triggerTemplate": {
"projectId": "my-project-id-1234",
"repoName": "github_example_repo",
"branchName": "master"
},
"description": "A sample build trigger",
"substitutions": {
"_TAG": "sample",
"_DOCKERFILE": "docker/Dockerfile",
"_MAJOR": "1",
"_MINOR": "0"
},
"ignoredFiles": [
".gitignore",
".dockerignore"
],
"filename": "cloudbuild.yaml"
}

This sample is a build trigger on commit to the master branch of a Cloud Source Repository, github_example_repo, a mirror of a GitHub repo. It includes four substitution variables that can be referenced in the cloudbuild.yaml file, which must be in the repository’s root.

Suggested workflow

  1. Create the first build trigger using the cloud console UI.
  2. Get the trigger ID from its URL — it’s the UUID in the path.
  3. Use get_build_trigger.sh to download the trigger JSON and save it to a file.
  4. Use the downloaded JSON as the base for new build triggers. (a) Remove the id and createTime fields from the JSON as these are output values only. (b) Create new triggers by submitting the new JSON file using create_build_trigger.sh, then overwrite the JSON file with the response which includes the new trigger’s ID, necessary for updating in the future.
  5. Check your triggers and scripts into source control.

--

--